package LeetCode.stackvsqueue;

import java.util.PriorityQueue;
//使用优先队列PriorityQueue,此堆的特点是最小元素在堆顶，每加入一个元素都是都进行一次排序，把最小元素放在堆顶
public class KthLargest {
    final PriorityQueue<Integer> q ;
    final int k;
    public KthLargest(int k ){
        this.k = k;
        q = new PriorityQueue<Integer>(k);
    }

    public int add(int[] a){
        for(int n:a){
            if(q.size()<k){
                q.offer(n);
            }
            else if(q.peek()<n){
                q.poll();
                q.offer(n);
            }
        }
        return q.peek();
    }


    public static void main(String[] args) {
        KthLargest kt =  new KthLargest(3);
        int[] a = {2,5,10,4,6,9,1,};
        int i = kt.add(a);
        System.out.println(i);
    }
}
